package com.RedRiver.hospitalsystem.personal_service.app.controller;


import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Subject;
import com.RedRiver.hospitalsystem.personal_service.service.Form.*;
import com.RedRiver.hospitalsystem.personal_service.service.SubjectService;
import com.RedRiver.hospitalsystem.personal_service.service.info.DepartmentInfo;
import com.RedRiver.hospitalsystem.personal_service.service.info.SubjectPageInfo;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@RestController
@RequestMapping("/subject")
@Api(tags = "科室管理接口")
public class SubjectController {

    @Resource
    private SubjectService subjectService;

    @ApiOperation("分页查看所有科室")
    @PostMapping("/findAllSubject")
    public Result findAllSubject(@RequestBody PageForm form){
        IPage<SubjectPageInfo> byPage = subjectService.findPageSubject(form);
        return  Result.getInstance(IPage.class).setMsg("分页成功").setData(byPage);
    }

    @ApiOperation("分页查看科室名相同的所有科室")
    @PostMapping("/findAllSubjectName")
    public Result findNameSubject(@RequestBody SubjectNamePageForm subjectNamePageForm){
        IPage<SubjectPageInfo> byPage = subjectService.findPageSubjectName(subjectNamePageForm);
        return  Result.getInstance(IPage.class).setMsg("分页成功").setData(byPage);
    }

    @ApiOperation("分页查看同一个部门下的所有科室")
    @PostMapping("/findAllSubjectDepartment")
    public Result findNameSubject(@RequestBody SubjectDepartmentPageForm subjectDepartmentPageForm){
        IPage<SubjectPageInfo> byPage = subjectService.findAllSubjectDepartment(subjectDepartmentPageForm);
        return  Result.getInstance(IPage.class).setMsg("分页成功").setData(byPage);
    }

    @ApiOperation("根据id查询科室信息")
    @PostMapping("/findSubjectById/{subjectId}")
    public Result findNameSubject(@PathVariable("subjectId")Integer subjectId){
        Subject subject_id = subjectService.getOne(new QueryWrapper<Subject>().eq("subject_id", subjectId));
        SubjectPageInfo subjectPageInfo = BeanUtil.copyProperties(subject_id, SubjectPageInfo.class);
        if (subjectPageInfo==null) return Result.getInstance(SubjectPageInfo.class).setData(subjectPageInfo).setMsg("查询失败");
        return Result.getInstance(SubjectPageInfo.class).setData(subjectPageInfo).setMsg("查询成功");
    }

    @ApiOperation("根据科室名查询科室")
    @PostMapping("/findSubjectName/{subjectName}")
    public Result findNameSubject(@PathVariable("subjectName") String subjectName){
        Subject subject_name = subjectService.getOne(new QueryWrapper<Subject>().eq("subject_name", subjectName));
        SubjectPageInfo subjectPageInfo = BeanUtil.copyProperties(subject_name, SubjectPageInfo.class);
        if (subjectPageInfo==null) return Result.getInstance(SubjectPageInfo.class).setData(subjectPageInfo).setMsg("查询失败");
        return Result.getInstance(SubjectPageInfo.class).setData(subjectPageInfo).setMsg("查询成功");
    }

    @ApiOperation("新增科室")
    @PostMapping("/createSubject")
    public Result createSubject(@RequestBody SubjectForm subjectForm){
        return subjectService.addSubject(subjectForm);
    }

    @ApiOperation("修改科室信息")
    @PostMapping("/updateSubject")
    public Result createSubject(@RequestBody NewSubjectForm newSubjectForm){
        return subjectService.changeSubject(newSubjectForm);
    }

    @ApiOperation("根绝id删除科室")
    @PostMapping("/updateSubject/{subjectId}")
    public Result delSubject(@ApiParam("请输入需要删除的Id") @PathVariable("subjectId")Integer subjectId){
        return subjectService.delSubject(subjectId);
    }

}

